class Category {
  final String id;
  final String name;
  final String? description;
  final List<Category> children;

  Category({
    required this.id,
    required this.name,
    this.description,
    this.children = const [],
  });

  factory Category.fromJson(Map<String, dynamic> json) {
    return Category(
      id: json['id'].toString(),
      name: (json['name'] as String?) ?? '未命名分类',
      description: json['description'] as String?,
      children: (json['children'] as List<dynamic>?)
              ?.map((child) => Category.fromJson(child as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'children': children.map((child) => child.toJson()).toList(),
    };
  }
}
